// pages/tabBar/search/my/my.js
import { request,formatFileSize } from '../.././../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {}, //用于储存用户信息
    defaltAvat: "/images/icon/icon.png", //未有微信头像时的默认头像
    message:[],   //消息
    count:"" ,     //消息数量
    percent:"",  //容量占比
    storage_size:"", //容量总大小
    used_size:"", //已用空间
    processStatus:"normal" //进度条状态
  },

  /**
   * 生命周期函数--监听页面加载
   * 获取用户信息
   */
  onLoad: function (options) {
    const that = this
    wx.getStorage({
      key: 'userInfo',
      success(res) {

        that.setData({
          userInfo: res.data
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面显示
   * 显示时获取用户信息
   */
  onShow: function () {
    const that = this
    wx.getStorage({
      key: 'userInfo',
      success(res) {

        that.setData({
          userInfo: res.data
        })
      }
    })
    request("GET","/api/auth/message/receiver/")
    .then(res =>{
      if(res.statusCode === 200){
        const list = res.data
        let count  = 0
        list.forEach( el =>{
          if(el.status === 0 && el.submit_action != 'copy'){
            count = count + 1
          }
        })
          that.setData({
            ...that.data,
            message:res.data,
            count:count
          })
      }
    })
    //个人容量
    request("GET","/api/auth/storage_user/")
    .then(res =>{
      if(res.statusCode === 200){
          const newUsedSize = formatFileSize(res.data.data[0].used_size)
          const newStorageSize = formatFileSize(res.data.data[0].storage_size)
          // const percent = res.data.data[0].percent >= 100 ? "100" : res.data.data[0].percent 
          const processStatus = res.data.data[0].percent >= 80 ? "wrong" : "active" 
          const availableCapacity =  res.data.data[0].storage_size - res.data.data[0].used_size
          wx.setStorageSync('availableCapacity', availableCapacity) //存储可用容量
          that.setData({
            ...that.data,
            percent:res.data.data[0].percent,
            used_size:newUsedSize,
            storage_size:newStorageSize,
            processStatus:processStatus
          })
      } 
    })
  },

  /**
   * 登出
   */
  logout: function () {
    wx.clearStorage();
    wx.reLaunch({
      url: '../../index/index',
    })
  },

  /**
   * 消息提示暂未开放的说明
   */
  msgNotify: function () {
    wx.showToast({
      title: '该功能暂未开放',
      icon: 'none',
      duration: 500
    })
  }
})